class Solution:
    def productExceptSelf(self, nums: list) -> list:
        length, mid_res = len(nums), 1
        res = [1] * length
        for i in range(length):
            res[i] = mid_res
            mid_res *= nums[i]
        j, mid_res = -1, 1
        while j >= -length:
            res[j] *= mid_res
            mid_res *= nums[j]
            j -= 1
        return res


class Solution:
    def productExceptSelf(self, nums: list) -> list:
        ji, length = 1, len(nums)
        zero_num = nums.count(0)
        if zero_num == 0:
            for num in nums:
                ji *= num
            res = []
            for i in range(length):
                res.append(ji // nums[i])
        elif zero_num >= 2:
            res = [0] * length
        else:
            index = nums.index(0)
            for num in nums:
                if num != 0:
                    ji *= num
            res = [0] * length
            res[index] = ji
        return res


a = Solution()
print(a.productExceptSelf([1, 2, 3, 4]))